package com.qxf.dao;

import com.qxf.pojo.Department;
import com.qxf.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.text.DateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author qxf101
 */
@Repository
public class EmployeeDao {
    private static Map<Integer, Employee> employees = null;
    @Autowired
    private DepartmentDao departmentDao;

    static {
        employees = new HashMap<>();

        employees.put(1, new Employee(1, "aa", "aa@qq.com", 1, new Department(101, "teach"), new Date()));
        employees.put(2, new Employee(2, "bb", "bb@qq.com", 0, new Department(102, "sport"), new Date()));
        employees.put(3, new Employee(3, "cc", "cc@qq.com", 1, new Department(103, "study"), new Date()));
    }

    private static Integer initId = 11;

    public void add(Employee employee) {
        if (employee.getId() == null) {
            employee.setId(initId++);
        }

        employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));
        employees.put(employee.getId(), employee);
    }

    public Collection<Employee> getAll() {
        return employees.values();
    }

    public Employee getEmployeeById(Integer id) {
        return employees.get(id);
    }

    public void delete(Integer id) {
        employees.remove(id);
    }
}
